package com.example.andengineScene;

import java.util.ArrayList;
import java.util.Date;

import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackTextureRegionLibrary;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackerTextureRegion;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Typeface;

import com.example.andengineConstant.GameConstant;
import com.example.andenginegame.MainActivity;
import com.example.andenginegame.Mission;
import com.example.andenginegame.MissionOfType;
import com.example.andenginegame.PlayData;
import com.example.andenginegame.Style;

public class MainScene extends Scene {
	
	
    private  Font titleFont;
    public   Text titleText;
    public   Sprite missionbackground;
    private  Font missionFont;
    private  Text missionText;
    private  Font scoreOfmissionFont;
    private  Text scoreOfmissionText;
    private  Font didMissionFont;
    private  Text didMissionText;
    private  Font scoreFont;
    private  Text scoreText;
    private  Font scoreNumberFont;
    private  Text scoreNumberText;
   
    private  Font maxScoreFont;
    private  Text maxScoreText;
    private  Font maxScoreNumberFont;
    private  Text maxScoreNumberText;

    private Sprite choseIconRound1;
    private Sprite choseIconRound2;
    
    private Sprite choseIconSprite;
    
    private Sprite doubleScorebackground;
    private Font doubleScoreFont;
    private Text doubleScoreText;
    
    private Sprite selectDoubleSprite;
    private Sprite unselectDoubleSprite;
    
    private Sprite playBackgroundSprite;
    private Sprite playIconSprite;
    
    private Sprite RankSprite;
    private Sprite myAchievementSprite;
    private Sprite onOfsoundSprite;
    private Sprite offOfsoundSprite;
    private Sprite QQSprite;
    public int didNum;
    public int needNum;
    public int score;
    public int maxScore;
    public int totalScore = 0;
    public int playNumber = 0;
    public int totalprefectNum = 0;
    public int totalplaytime = 0;
    public int totalprectStart = 0;
    public int oncePlayTime =0;
    public int onecPrefectNum = 0;
    public int totalplayPrefectNum = 0;
    public Style style;
    public Mission mission;
    private int missionId;
    public Date date;
    
    public int getMissionId() {
		return missionId;
	}

    private MainActivity act;
	public void setMissionId(int missionId) {
		this.missionId = missionId;
	}
	ArrayList<Mission> missis;
    ArrayList<MissionOfType> missiTypes;
    PlayScene playScene;
    public PlayData playData;
	public Style getStyle() {
	return style;
        }


public void setStyle(Style style) {
	this.style = style;
}


	public Font getTitleFont() {
		return titleFont;
	}


	public void setTitleFont(Font titleFont) {
		this.titleFont = titleFont;
		 this.titleFont.load();
	}



	public Text getTitleText() {
		return titleText;
	}


	public void setTitleText(Text titleText) {
		this.titleText = titleText;
		this.titleText.setScale(act.xscale, act.yscale);
		this.attachChild(titleText);
	}


	public Sprite getMissionbackground() {
		return missionbackground;
	}


	public void setMissionbackground(Sprite missionbackground) {
		this.missionbackground = missionbackground;
		this.missionbackground.setScale(act.xscale, act.yscale);
		this.attachChild(this.missionbackground);
	}


	public Font getMissionFont() {
		return missionFont;
	}



	public void setMissionFont(Font missionFont) {
		this.missionFont = missionFont;
		this.missionFont.load();
	}


	public Text getMissionText() {
		return missionText;
	}



	public void setMissionText(Text missionText) {
		this.missionText = missionText;
		
		this.attachChild(this.missionText);
	}



	public Font getScoreOfmissionFont() {
		return scoreOfmissionFont;
	}



	public void setScoreOfmissionFont(Font scoreOfmissionFont) {
		this.scoreOfmissionFont = scoreOfmissionFont;
		this.scoreOfmissionFont.load();
	}



	public Text getScoreOfmissionText() {
		return scoreOfmissionText;
	}



	public void setScoreOfmissionText(Text scoreOfmissionText) {
		this.scoreOfmissionText = scoreOfmissionText;
		
		this.attachChild(this.scoreOfmissionText);
	}



	public Font getDidMissionFont() {
		return didMissionFont;
	}



	public void setDidMissionFont(Font didMissionFont) {
		this.didMissionFont = didMissionFont;
		this.didMissionFont.load();
	}



	public Text getDidMissionText() {
		return didMissionText;
	}



	public void setDidMissionText(Text didMissionText) {
		this.didMissionText = didMissionText;
		
		this.attachChild(this.didMissionText);
	}


	public Font getScoreFont() {
		return scoreFont;
	}


	public void setScoreFont(Font scoreFont) {
		this.scoreFont = scoreFont;
		this.scoreFont.load();
	}



	public Text getScoreText() {
		return scoreText;
	}


	public void setScoreText(Text scoreText) {
		this.scoreText = scoreText;
	
		this.attachChild(this.scoreText);
	}


	public Font getScoreNumberFont() {
		return scoreNumberFont;
	}


	public void setScoreNumberFont(Font scoreNumberFont) {
		this.scoreNumberFont = scoreNumberFont;
		this.scoreNumberFont.load();
	}


	public Text getScoreNumberText() {
		return scoreNumberText;
	}



	public void setScoreNumberText(Text scoreNumberText) {
		this.scoreNumberText = scoreNumberText;
	
		this.attachChild(this.scoreNumberText);
	}


	public Font getMaxScoreFont() {
		return maxScoreFont;
	}



	public void setMaxScoreFont(Font maxScoreFont) {
		this.maxScoreFont = maxScoreFont;
		this.maxScoreFont.load();
	}


	public Text getMaxScoreText() {
		return maxScoreText;
	}



	public void setMaxScoreText(Text maxScoreText) {
		this.maxScoreText = maxScoreText;
		
		this.attachChild(maxScoreText);
	}



	public Font getMaxScoreNumberFont() {
		return maxScoreNumberFont;
	}



	public void setMaxScoreNumberFont(Font maxScoreNumberFont) {
		this.maxScoreNumberFont = maxScoreNumberFont;
		this.maxScoreNumberFont.load();
	}



	public Text getMaxScoreNumberText() {
		return maxScoreNumberText;
	}



	public void setMaxScoreNumberText(Text maxScoreNumberText) {
		this.maxScoreNumberText = maxScoreNumberText;
		
		this.attachChild(this.maxScoreNumberText);
	}


	public Sprite getChoseIconRound1() {
		return choseIconRound1;
	}

	public void setChoseIconRound1(Sprite choseIconRound1) {
		this.choseIconRound1 = choseIconRound1;
		
		this.attachChild(choseIconRound1);
	}


	public Sprite getChoseIconRound2() {
		return choseIconRound2;
	}


	public void setChoseIconRound2(Sprite choseIconRound2) {
		this.choseIconRound2 = choseIconRound2;
	
		this.attachChild(this.choseIconRound2);
	}


	public Sprite getChoseIconSprite() {
		return choseIconSprite;
	}


	public void setChoseIconSprite(Sprite choseIconSprite) {
		this.choseIconSprite = choseIconSprite;
		
		this.attachChild(this.choseIconSprite);
		this.registerTouchArea(this.choseIconSprite);
	}


	public Sprite getDoubleScorebackground() {
		return doubleScorebackground;
	}


	public void setDoubleScorebackground(Sprite doubleScorebackground) {
		this.doubleScorebackground = doubleScorebackground;
		this.doubleScorebackground.setScale(act.xscale, act.yscale);
		this.attachChild(this.doubleScorebackground);
	}


	public Font getDoubleScoreFont() {
		return doubleScoreFont;
	}



	public void setDoubleScoreFont(Font doubleScoreFont) {
		this.doubleScoreFont = doubleScoreFont;
		this.doubleScoreFont.load();
	}


	public Text getDoubleScoreText() {
		return doubleScoreText;
	}



	public void setDoubleScoreText(Text doubleScoreText) {
		this.doubleScoreText = doubleScoreText;
	
		this.attachChild(this.doubleScoreText);
	}



	public Sprite getSelectDoubleSprite() {
		return selectDoubleSprite;
	}



	public void setSelectDoubleSprite(Sprite selectDoubleSprite) {
		this.selectDoubleSprite = selectDoubleSprite;
	}


	public Sprite getUnselectDoubleSprite() {
		return unselectDoubleSprite;
	}


	public void setUnselectDoubleSprite(Sprite unselectDoubleSprite) {
		this.unselectDoubleSprite = unselectDoubleSprite;
		
		this.attachChild(this.unselectDoubleSprite);
	}



	public Sprite getPlayBackgroundSprite() {
		return playBackgroundSprite;
	}


	public void setPlayBackgroundSprite(Sprite playBackgroundSprite) {
		this.playBackgroundSprite = playBackgroundSprite;
		this.playBackgroundSprite.setScale(act.xscale, act.yscale);
		this.attachChild(this.playBackgroundSprite);
		this.registerTouchArea(this.playBackgroundSprite);
	}


	public Sprite getPlayIconSprite() {
		return playIconSprite;
	}


	public void setPlayIconSprite(Sprite playIconSprite) {
		this.playIconSprite = playIconSprite;
		this.playIconSprite.setScale(act.xscale, act.yscale);
		this.attachChild(this.playIconSprite);
	}


	public Sprite getRankSprite() {
		return RankSprite;
	}



	public void setRankSprite(Sprite rankSprite) {
		RankSprite = rankSprite;
	}



	public Sprite getMyAchievementSprite() {
		return myAchievementSprite;
	}




	public void setMyAchievementSprite(Sprite myAchievementSprite) {
		this.myAchievementSprite = myAchievementSprite;
	}



	public Sprite getOnOfsoundSprite() {
		return onOfsoundSprite;
	}



	public void setOnOfsoundSprite(Sprite onOfsoundSprite) {
		this.onOfsoundSprite = onOfsoundSprite;
	}


	public Sprite getOffOfsoundSprite() {
		return offOfsoundSprite;
	}

	public void setOffOfsoundSprite(Sprite offOfsoundSprite) {
		this.offOfsoundSprite = offOfsoundSprite;
	}


	public Sprite getQQSprite() {
		return QQSprite;
	}


	public void setQQSprite(Sprite qQSprite) {
		QQSprite = qQSprite;
	}

	public MainScene(final MainActivity act, int choseId,
			final ArrayList<Style> styles, ArrayList<Mission> missions, final int missionId, ArrayList<MissionOfType> missionTypes) {
     		this.act = act;
	    //璁剧疆鏍囬
     	 
     	 setMissionId(missionId);
     	 missiTypes = missionTypes;
     	 missis = missions;
     	 mission = missions.get(missionId);
		 style = styles.get(choseId);
		 playData = new PlayData();
		 
	     this.setBackground(new Background(style.getBg_r(), style.getBg_g(),style.getBg_b()));
		 this.titleFont =  FontFactory.create(act.getFontManager(), act.getTextureManager(), 
					484, 148, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 120,  Color.WHITE.getARGBPackedInt());
		
		 setTitleFont(titleFont);
        
		 titleText = new Text((act.CAMERA_WIDTH-titleFont.getTexture().getWidth())/2,
				200, this.titleFont, "疯狂撞击", new TextOptions(HorizontalAlign.LEFT), act.getVertexBufferObjectManager());
	     setTitleText(titleText);
	     titleText.setPosition(act.CAMERA_WIDTH/2-titleText.getWidth()/2,act.CAMERA_HEIGH/7);
	     this.getTitleText().setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
	    //璁剧疆浠诲姟搴曟
	    TextureRegion missionTexture = act.ur3.get(23);
	        
		
	 	missionbackground = new Sprite(0,350,missionTexture,act.getVertexBufferObjectManager());
	 	missionbackground.setColor(GameConstant.UNDER_COLOR_R,GameConstant.UNDER_COLOR_G,GameConstant.UNDER_COLOR_B);
	 	missionbackground.setPosition(act.CAMERA_WIDTH/2-missionbackground.getWidth()/2, titleText.getY()+titleText.getHeight()+act.CAMERA_WIDTH/10);
	 	setMissionbackground(missionbackground);
	 	//璁剧疆浠诲姟鎻愮ず
	 	String missionString = GameConstant.MISSIONNAME+this.getMissionId()+GameConstant.MISSIONNUM_PID;
	 
	 	missionFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
				512, 512, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 20, Color.WHITE.getARGBPackedInt());
		setMissionFont(missionFont);
		
	 	missionText = new Text(50,370,
		this.missionFont, missionString,Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	    missionText.setPosition(missionbackground.getX()+20, missionbackground.getY()+10);
	    
	 	setMissionText(missionText);
	   
	    scoreOfmissionFont = FontFactory.create(act.getFontManager(),act.getTextureManager(), 
				1024, 1024, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 30, Color.WHITE.getARGBPackedInt());
	    setScoreOfmissionFont(scoreOfmissionFont);
	    setDidMissionFont(scoreOfmissionFont);
	    String missionType = missions.get(this.missionId-1).getType();
	    String desc = "";
	    
	    for (int i = 0; i < missionTypes.size(); i++) {
			if(missionTypes.get(i).getType().equals(missionType))
				desc = missionTypes.get(i).getDesc();
		 }
	    mission = missions.get(this.missionId-1);
	    //寰楀埌闇�瀹屾垚鐨勪换鍔￠噺
	     getMissionNeedNum(missionType);
	   
	     
	 
	     scoreOfmissionText = new Text(missionbackground.getX()+20,missionbackground.getY()+missionbackground.getHeight()-missionbackground.getHeight()-20,
				 this.scoreOfmissionFont, desc.replace("{0}",needNum+""),Character.MAX_VALUE, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	     scoreOfmissionText.setPosition(missionbackground.getX()+20,missionbackground.getY()+missionbackground.getHeight()-scoreOfmissionText.getHeight()-10);
	     setScoreOfmissionText(scoreOfmissionText);
	     didNum =act.missionFinishNum;
	     String didString = didNum+"/"+needNum;
	    
	     didMissionText = new Text(this.missionbackground.getX()+this.missionbackground.getWidth(),385,
				 this.didMissionFont, didString,Character.MAX_RADIX, new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	     didMissionText.setPosition(missionbackground.getX()+missionbackground.getWidth()-didMissionText.getWidth()-20, missionbackground.getY()+missionbackground.getHeight()/2-didMissionText.getHeight()/2);
	     setDidMissionText(didMissionText);
	     this.didMissionText.setPosition(this.missionbackground.getX()+this.missionbackground.getWidth()-this.didMissionText.getWidth()-40,this.didMissionText.getY());
	     //璁剧疆寰楀垎
	      scoreFont  = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
					119, 62, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 48,  Color.WHITE.getARGBPackedInt());
	  	  setScoreFont(scoreFont);
	      scoreText= new Text(act.CAMERA_WIDTH/2-50,act.CAMERA_HEIGH/2,
				 this.scoreFont, "得分", new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
	      scoreText.setPosition(act.CAMERA_WIDTH/2-scoreText.getWidth()/2, missionbackground.getY()+missionbackground.getHeight()+act.CAMERA_HEIGH/15);
	      setScoreText(scoreText);
	      this.getScoreText().setColor(style.getTarget_r(),style.getTarget_g(),style.getTarget_b());
		  scoreNumberFont = FontFactory.create(act.getFontManager(), act.getTextureManager(), 
					512, 512, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 50,  Color.WHITE.getARGBPackedInt());
		  setScoreNumberFont(scoreNumberFont);
	      score = act.score;
		  scoreNumberText = new Text(act.CAMERA_WIDTH/2-15,act.CAMERA_HEIGH/2+50,
				 this.scoreNumberFont, score+"", Character.MAX_VALUE, act.getVertexBufferObjectManager());
	      scoreNumberText.setPosition(act.CAMERA_WIDTH/2-scoreNumberText.getWidth()/2, scoreText.getY()+scoreText.getHeight()+act.CAMERA_HEIGH/30);
		  setScoreNumberText(scoreNumberText);
	      maxScoreFont = FontFactory.create(act.getFontManager(),act.getTextureManager(), 
	    		  512, 512, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 30,  Color.WHITE.getARGBPackedInt());
		  setMaxScoreFont(maxScoreFont);	
		  maxScoreNumberFont = FontFactory.create(act.getFontManager(),act.getTextureManager(), 
					119, 40, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 30,  Color.WHITE.getABGRPackedInt());
	
		  setMaxScoreNumberFont(maxScoreNumberFont);
	      maxScoreText= new Text(act.CAMERA_WIDTH/2-70,act.CAMERA_HEIGH/2+130,
				 this.maxScoreFont, "最佳",  act.getVertexBufferObjectManager());
	      maxScoreText.setPosition(act.CAMERA_WIDTH/2-maxScoreText.getWidth(),scoreNumberText.getY()+scoreNumberText.getHeight()+act.CAMERA_HEIGH/90);
	      setMaxScoreText(maxScoreText);
	      this.getMaxScoreText().setColor(style.getTarget_r(),style.getTarget_g(),style.getTarget_b());
	      maxScore = act.maxScore;
		  maxScoreNumberText = new Text(act.CAMERA_WIDTH/2,act.CAMERA_HEIGH/2+130,
				 this.maxScoreNumberFont, maxScore+"", "FPS: XXXXX".length(), act.getVertexBufferObjectManager());
		  maxScoreNumberText.setPosition(act.CAMERA_WIDTH/2,maxScoreText.getY());

		  setMaxScoreNumberText(maxScoreNumberText);
	     //璁剧疆閫夋嫨鍥炬爣鐨勬寜閽�
	     TextureRegion IconRound = act.ur3.get(36);
	 	 choseIconRound1 = new Sprite(act.CAMERA_WIDTH/10,act.CAMERA_HEIGH/2,IconRound,act.getVertexBufferObjectManager());
		 choseIconRound1.setPosition(act.CAMERA_WIDTH/12,missionbackground.getY()+missionbackground.getHeight()+act.CAMERA_HEIGH/13);
	 	 choseIconRound2 = new Sprite(act.CAMERA_WIDTH/10,missionbackground.getY()+missionbackground.getHeight()+act.CAMERA_HEIGH/15,IconRound,act.getVertexBufferObjectManager());
	    
	     
	     final LoopEntityModifier entityModifier =
	 			new LoopEntityModifier(
	 				
	 					new SequenceEntityModifier(
 							
	 							new ScaleModifier(2, 0.7f,1)
						
	 					)
	 			);
	     choseIconRound2.setPosition(act.CAMERA_WIDTH/12,missionbackground.getY()+missionbackground.getHeight()+act.CAMERA_HEIGH/13);
	     choseIconRound1.registerEntityModifier(entityModifier);
	 	 setChoseIconRound1(choseIconRound1);
		 setChoseIconRound2(choseIconRound2);
		 
		 //寰楀埌閫夋嫨鐨勫浘鏍�
			TexturePackTextureRegionLibrary texLibraryPlay = act.ur3;
			if(GameConstant.paly_whichOfImage[style.getId()]==3)
				texLibraryPlay= act.ur3;
			if(GameConstant.paly_whichOfImage[style.getId()]==1)
				texLibraryPlay= act.ur1;
			if(GameConstant.paly_whichOfImage[style.getId()]==2)
				texLibraryPlay= act.ur2;
		 TexturePackerTextureRegion choseIconTexture = texLibraryPlay.get(GameConstant.play_idofImage[style.getId()]);
	 
		 
		 choseIconSprite = new Sprite(choseIconRound1.getX()+choseIconRound1.getWidth()/2,act.CAMERA_HEIGH/2+50,choseIconTexture,act.getVertexBufferObjectManager()){
				public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
		        {
		           if (touchEvent.isActionUp())

		           {	
		           	if(act.mEng.isRunning()) {
                            
                          Scene selectScene = new SelectSpriteScene(act,missionId,styles);
                            act.mEng.setScene(selectScene);
		            	
		    			} else {
		    			
		    				act.mEng.start();
		    			}
		    			return true;
		            }
		            return true;
		         };
			};
		 choseIconSprite.setPosition(choseIconRound1.getX()+choseIconRound1.getWidth()/2-choseIconSprite.getWidth()/2,choseIconRound1.getY()+choseIconRound1.getHeight()/2-choseIconSprite.getHeight()/2);
         choseIconSprite.setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
		 setChoseIconSprite(choseIconSprite);
		 
		 TexturePackerTextureRegion doubleScorebackgroundTexture = act.ur3.get(2);
		
		 
		 TexturePackerTextureRegion unIconSelect = act.ur3.get(22);
		 unselectDoubleSprite = new Sprite(act.CAMERA_WIDTH/2+80,act.CAMERA_HEIGH/2+87,unIconSelect,act.getVertexBufferObjectManager());
		 unselectDoubleSprite.setColor(style.getTarget_r(),style.getTarget_g(),style.getTarget_b());
		 
		 doubleScoreFont = 	 FontFactory.create(act.getFontManager(), act.getTextureManager(), 
					264, 47, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 36,  Color.WHITE.getARGBPackedInt());
		 setDoubleScoreFont(doubleScoreFont);	
		 doubleScoreText = new Text(act.CAMERA_WIDTH/2+120,act.CAMERA_HEIGH/2+87,
				 this.doubleScoreFont, "双倍得分", new TextOptions(HorizontalAlign.CENTER), act.getVertexBufferObjectManager());
		
		 doubleScorebackground = new Sprite(act.CAMERA_WIDTH/2+70,act.CAMERA_HEIGH/2+50,doubleScorebackgroundTexture,act.getVertexBufferObjectManager());
		 doubleScorebackground.setColor(GameConstant.UNDER_COLOR_R, GameConstant.UNDER_COLOR_G, GameConstant.UNDER_COLOR_B);
		 doubleScorebackground.setPosition(act.CAMERA_WIDTH/2-act.CAMERA_WIDTH/12-choseIconRound1.getWidth()+act.CAMERA_WIDTH/2, choseIconRound1.getY()+choseIconRound1.getHeight()/2-doubleScorebackground.getHeight()/2);
		 unselectDoubleSprite.setPosition(doubleScorebackground.getX()+act.CAMERA_WIDTH/40, doubleScorebackground.getY()+doubleScorebackground.getHeight()/2-unselectDoubleSprite.getHeight()/2);
		 doubleScoreText.setPosition(unselectDoubleSprite.getX()+unselectDoubleSprite.getWidth()+act.CAMERA_WIDTH/60, doubleScorebackground.getY()+doubleScorebackground.getHeight()/2-doubleScoreText.getHeight()/2);
		 setDoubleScorebackground(doubleScorebackground);
		 setDoubleScoreText(doubleScoreText);
		 setUnselectDoubleSprite(unselectDoubleSprite);	
		 
		 
		 
		 this.getDoubleScoreText().setColor(style.getTarget_r(),style.getTarget_g(),style.getTarget_b());
		 final float playIconX  =  (act.CAMERA_WIDTH-doubleScorebackground.getWidth())/2;
		 playBackgroundSprite = new Sprite(playIconX,act.CAMERA_HEIGH/2+200,doubleScorebackgroundTexture,act.getVertexBufferObjectManager()){
		 public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)

		        {
		            if (touchEvent.isActionUp())

		            {

		            	if(act.mEng.isRunning()) {
		            		 act.clickSound.play();
		        			 playScene = new PlayScene(act);
		        			 playScene.setStyle(act.mainScene.getStyle());
		        			 act.mEng.setScene(playScene);
		    			} else {
		    			
		    				act.mEng.start();
		    			}
		    			return true;

		            }

		            
		            return true;

		        }
			};
			playBackgroundSprite.setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
			playBackgroundSprite.setPosition(act.CAMERA_WIDTH/2-playBackgroundSprite.getWidth()/2, act.CAMERA_HEIGH*6/8);
			setPlayBackgroundSprite(playBackgroundSprite);	
			
			TextureRegion playIcon = act.ur1.get(30);
			final float playIX  =  (act.CAMERA_WIDTH-playIcon.getWidth())/2;
			playIconSprite = new Sprite(playIX,act.CAMERA_HEIGH/2+200+doubleScorebackgroundTexture.getHeight()/4,playIcon,act.getVertexBufferObjectManager());
			playIconSprite.setColor(Color.WHITE);
			playIconSprite.setPosition(act.CAMERA_WIDTH/2-playIconSprite.getWidth()/2, playBackgroundSprite.getY()+playBackgroundSprite.getHeight()/2-playIconSprite.getHeight()/2);
			setPlayIconSprite(playIconSprite);
			TextureRegion ranking = act.ur3.get(15);
			Sprite rankSprite = new Sprite(100,act.CAMERA_HEIGH-100,ranking,act.getVertexBufferObjectManager());
			rankSprite.setPosition(act.CAMERA_WIDTH/8-rankSprite.getWidth()/2, act.CAMERA_HEIGH-rankSprite.getHeight()-30);
			rankSprite.setScale(act.xscale, act.yscale);
			this.attachChild(rankSprite);
			
			TextureRegion QQ = act.ur3.get(18);
			Sprite QQSprite = new Sprite(400,act.CAMERA_HEIGH-100,QQ,act.getVertexBufferObjectManager());
			QQSprite.setPosition(act.CAMERA_WIDTH*3/8-QQSprite.getWidth()/2, act.CAMERA_HEIGH-rankSprite.getHeight()-30);
			QQSprite.setScale(act.xscale, act.yscale);
			this.attachChild(QQSprite);
			TextureRegion Sound = act.ur3.get(20);
			Sprite soundSprite = new Sprite(300,act.CAMERA_HEIGH-100,Sound,act.getVertexBufferObjectManager());
			soundSprite.setPosition(act.CAMERA_WIDTH*5/8-soundSprite.getWidth()/2, act.CAMERA_HEIGH-rankSprite.getHeight()-30);
			soundSprite.setScale(act.xscale, act.yscale);
			this.attachChild(soundSprite);
			TextureRegion Icheve = act.ur3.get(13);
			Sprite IcheveSprite = new Sprite(200,act.CAMERA_HEIGH-100,Icheve,act.getVertexBufferObjectManager()){
				 public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)

			        {
			            if (touchEvent.isActionUp())

			            {

			            	if(act.mEng.isRunning()) {
			            		
			            		act.clickSound.play();
			            		 MyScoreScene myscene = new MyScoreScene(act);
			        			
			        			 act.mEng.setScene(myscene);
			    			} else {
			    			
			    				act.mEng.start();
			    			}
			    			return true;

			            }

			            return true;

			        }
				};
			IcheveSprite.setScale(act.xscale, act.yscale);
			this.attachChild(IcheveSprite);
			IcheveSprite.setPosition(act.CAMERA_WIDTH*7/8-IcheveSprite.getWidth()/2, act.CAMERA_HEIGH-rankSprite.getHeight()-30);

			this.registerTouchArea(IcheveSprite);
			
			
	}
 	public void restScene() {
 		
 		judgeDidMission();
 	  
 		 this.getTitleText().setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
 		 //寰楀埌閫夋嫨鐨勫浘鏍�
			TexturePackTextureRegionLibrary texLibraryPlay = act.ur3;
			if(GameConstant.paly_whichOfImage[style.getId()]==3)
				texLibraryPlay= act.ur3;
			if(GameConstant.paly_whichOfImage[style.getId()]==1)
				texLibraryPlay= act.ur1;
			if(GameConstant.paly_whichOfImage[style.getId()]==2)
				texLibraryPlay= act.ur2;
		 TexturePackerTextureRegion choseIconTexture = texLibraryPlay.get(GameConstant.play_idofImage[style.getId()]);
	     
		 choseIconSprite.detachSelf();
		 choseIconSprite = null;
		 choseIconSprite = new Sprite(act.CAMERA_WIDTH/2-200,act.CAMERA_HEIGH/2+50,choseIconTexture,act.getVertexBufferObjectManager());
         choseIconSprite.setPosition(choseIconRound1.getX()+choseIconRound1.getWidth()/2-choseIconSprite.getWidth()/2,choseIconRound1.getY()+choseIconRound1.getHeight()/2-choseIconSprite.getHeight()/2);
		 choseIconSprite.setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
         setChoseIconSprite(choseIconSprite);
         playBackgroundSprite.setColor(style.getMe_r(), style.getMe_g(), style.getMe_b());
     	 this.setBackground(new Background(style.getBg_r(), style.getBg_g(), style.getBg_b()));
         if(didNum>=needNum)
         {    
        	 playData = new PlayData();
        	 setMissionId(this.getMissionId()+1);
        	 mission = missis.get(this.getMissionId()-1);
        	 didNum = 0;
        	 String missionType = mission.getType();
    	     //寰楀埌闇�瀹屾垚鐨勪换鍔￠噺
        	 getMissionNeedNum(missionType);      	
        	 String missionString = GameConstant.MISSIONNAME+this.missionId+GameConstant.MISSIONNUM_PID;
        	 this.missionText.setText(missionString);
        	 String desc = "";
        	 for (int i = 0; i < missiTypes.size(); i++) {
      			if(missiTypes.get(i).getType().equals(missionType))
      				desc= missiTypes.get(i).getDesc();
      		 }

        	 this.scoreOfmissionText.setText(desc.replace("{0}", needNum+""));
     		 
         }

	    this.scoreNumberText.setText(this.score+"");
	    this.scoreNumberText.setPosition(act.CAMERA_WIDTH/2-scoreNumberText.getWidth()/2, scoreText.getY()+scoreText.getHeight()+10);
	    this.maxScoreNumberText.setText(this.maxScore+"");
		this.didMissionText.setText(this.didNum+"/"+needNum);	
		this.didMissionText.setPosition(this.missionbackground.getX()+this.missionbackground.getWidth()-this.didMissionText.getWidth()-40, this.didMissionText.getY());
		saveLocalData();//淇濆瓨娓告垙鏁版嵁
		 
	
	}
    private void saveLocalData() {
    	SharedPreferences sharedPreferences = act.getSharedPreferences("SP", 
				 Activity.MODE_PRIVATE); 
		
		 SharedPreferences.Editor editor = sharedPreferences.edit(); 
			
	     editor.putInt(GameConstant.SCORE, this.score); 
		 editor.putInt(GameConstant.MAXSCORE, this.maxScore); 
		 editor.putInt(GameConstant.MISSION_ID,this.mission.getId());
		 editor.putInt(GameConstant.CHOSETYPEID,this.style.getId());
		 editor.putInt(GameConstant.MISSIONFINISHNUM,didNum);
		 editor.putString(GameConstant.TODAT_DATE, act.sf.format(new Date()));
		 editor.putInt(GameConstant.TOTAL_SCORE, act.totalScore);
		 editor.putInt(GameConstant.TODAY_SCORE, act.todayScore);
		 editor.putInt(GameConstant.TOTAL_PLAYGAMENUM, act.totalPlayGameNum);
		 editor.putInt(GameConstant.TODAY_PLAYGAMENUM, act.todayPlayGameNum);
		 editor.putInt(GameConstant.TOTAL_PREFECTNUM, act.totalPrefectNum);
		 editor.putInt(GameConstant.TODAY_PREFECTNUM, act.todayPrefectNum);
		 editor.commit(); 
		
	}


	/**
     * 閫氳繃浠诲姟绫诲瀷寰楀埌浠诲姟閲�
     * @param missionType
     */
 	private void getMissionNeedNum(String missionType) {
 		 if(missionType.equals("101"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("102"))
	     {
	    	 needNum = mission.getNeedNum()*10;
	     }
	     if(missionType.equals("103"))
	     {
	    	 needNum = mission.getNeedNum()*10;
	     }
	     if(missionType.equals("104"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("105"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("106"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("107"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("108"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("109"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("110"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("111"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     if(missionType.equals("112"))
	     {
	    	 needNum = mission.getNeedNum();
	     }
	     
		System.out.println("浠诲姟锛�"+missionType);
	}


	/**
 	 * 鑾峰緱浠诲姟鐨勫畬鎴愰噺   鍜屽皢涓�簺鏁版嵁褰掗浂
 	 */

	private void judgeDidMission() {
		   if(mission.getType().equals("101"))
		    	didNum = playData.getScore();
		    if(mission.getType().equals("102"))
		    	didNum = playData.getPlay_one_game_score();
		    
		    if(mission.getType().equals("103"))
		    	didNum = playData.getPlay_addup_game_score();
		    if(mission.getType().equals("104"))
		    	didNum = playData.getPlay_game_num();
		    if(mission.getType().equals("105"))
		    	didNum = playData.getPlay_one_game_prefectNum();
		    if(mission.getType().equals("106"))
		    	didNum = playData.getPlay_addup_game_prefectNum();
		    if(mission.getType().equals("107"))
		    	didNum = (int) playData.getPlay_one_game_time();
		    if(mission.getType().equals("108"))
		    	didNum = (int) playData.getPlay_addup_game_time();
		    if(mission.getType().equals("109"))
		    	didNum = playData.getPrefect_start_num();
		    if(mission.getType().equals("110"))
		    	didNum = playData.getPlay_continue_game_num();
		    if(mission.getType().equals("111"))
		    	didNum = playData.getGet_continue_prefectNum();
		    if(mission.getType().equals("112"))
		    	didNum = playData.getGet_prefectNum();
	 		
	 		 playData.setPlay_one_game_score(0);
	 		 playData.setPlay_one_game_prefectNum(0);
	 		 playData.setPlay_one_game_time(0);
	 		 playData.setGet_continue_prefectNum(0);
		
	}

}
